package cn.wizzer.framework.util;

import org.nutz.lang.Times;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

public class DateUtil {

    /**
     * 字符串转Date
     *
     * @param s
     * @param pattern
     * @return
     */
    public static Date parseDate(String s, String pattern) {
        try {
            return new SimpleDateFormat(pattern, Locale.US).parse(s);
        } catch (ParseException ignore) {
            return null;
        }
    }

    /**
     * 时区转换。支持冬令时、夏令时计算
     *
     * @param date           初始Date
     * @param initTimezone   原时区
     * @param expectTimeZone 要转的时区
     * @return Date
     */
    public static String timeZoneConvert(Date date, String initTimezone, String expectTimeZone) {
        try {
            SimpleDateFormat simpleDate = new SimpleDateFormat(DATE_FORMAT_PATTERN.Y_M_D_H_M_S.PATTERN);
            // 将Date转String
            String dateStr = simpleDate.format(date);
            // 设置时区
            simpleDate.setTimeZone(TimeZone.getTimeZone(initTimezone));
            // 使Date关联初始时区initTimeZone
            date = simpleDate.parse(dateStr);
            // 设置要转换的时区
            simpleDate.setTimeZone(TimeZone.getTimeZone(expectTimeZone));
            // 通过转换字符串使Date关联要转换的时区expectTimeZone
            return simpleDate.format(date);
        } catch (ParseException e) {
            return null;
        }
    }

    /**
     * 转美东时区Date
     *
     * @param time
     * @return
     */
    public static Date sysConvertEdt(long time) {
        String s = sysConvertEdt(Times.ts2D(time));
        return parseDate(s, DATE_FORMAT_PATTERN.Y_M_D_H_M_S.PATTERN);
    }


    /**
     * 转美东时区DateString
     *
     * @param date
     * @return
     */
    public static String sysConvertEdt(Date date) {
        return DateUtil.timeZoneConvert(date, TimeZone.getDefault().getID(), "America/New_York");
    }


    public static Date edtConvertSys(Date date) {
        String s = timeZoneConvert(date, "America/New_York", TimeZone.getDefault().getID());
        return parseDate(s, DATE_FORMAT_PATTERN.Y_M_D_H_M_S.PATTERN);
    }

    /**
     * 客户端时区时间转系统时区时间
     *
     * @param timezone 客户端时区id
     * @param time     时间戳(秒级)
     * @return 秒级
     */
    public static long timeZoneConvertSys(String timezone, long time) {
        String dateStr = timeZoneConvert(new Date(time * 1000L), timezone, TimeZone.getDefault().getID());
        return Times.ams(dateStr) / 1000;
    }


    /**
     * 某天零时的long值
     *
     * @param offset   0(今天零点) +1(明天零点) -1(昨天零点) +2(后天零点) ....
     * @param timezone
     * @return
     */
    public static long getZeroLong(int offset, String timezone) {
        // 指定时区当天加减偏移量
        Calendar cal = Calendar.getInstance();
        cal.add(Calendar.DAY_OF_MONTH, offset);
        // 系统时间转成指定时区时间
        String timezoneDateStr = timeZoneConvert(cal.getTime(), TimeZone.getDefault().getID(), timezone);
        String timeZoneZeroStr = timezoneDateStr.split(" ")[0] + " 00:00:00";
        // 获得加减偏移量后的那天零点long值
        long ams = Times.ams(timeZoneZeroStr, TimeZone.getTimeZone(timezone));
        return ams / 1000L;
    }


    public static String dateFormatByTimeZone(String timezone, String format, long time) {
        SimpleDateFormat simpleDate = new SimpleDateFormat(format);
        simpleDate.setTimeZone(TimeZone.getTimeZone(timezone));
        return simpleDate.format(new Date(time));
    }


    public enum DATE_FORMAT_PATTERN {
        // 2020-01-01 00:00:00
        Y_M_D_H_M_S("yyyy-MM-dd HH:mm:ss"),
        // 2020年01月01日00:00:00
        Y_M_D_H_M_S_CHINESE("yyyy年MM月dd日HH:mm:ss"),
        // 2020-01-01 00:00am
        Y_M_D_H_M_A("yyyy-MM-dd hh:mma"),
        // 2020-01-01
        Y_M_D("yyyy-MM-dd");
        public final String PATTERN;

        DATE_FORMAT_PATTERN(String pattern) {
            this.PATTERN = pattern;
        }
    }
}
